﻿using AiMoBridge.Interface;
using AiMoWindows.Windows.CommandLibrary.ViewModels;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace AiMoWindows.Windows.CommandLibrary
{
    /// <summary>
    /// CommandLibraryWindow.xaml 的交互逻辑
    /// </summary>
    partial class CommandLibraryWindow : Window, IAiMoWindow
    {
        readonly CommandLibraryViewModel m_ViewModel;
        public CommandLibraryWindow(IExecutor? executor)
        {
            m_ViewModel = new CommandLibraryViewModel(this, executor);
            DataContext = m_ViewModel;
            InitializeComponent();
            AddEvent();
            m_ViewModel.Onload();
        }
        void AddEvent()
        {
            ComandItemsListView.PreviewMouseWheel += m_ViewModel.CommanLibraryUI.MouseWheelChangeSize;
            OutputTextBox.PreviewMouseWheel += m_ViewModel.CommanLibraryUI.MouseWheelChangeSize;
        }
        void RemoveEvent()
        {
            ComandItemsListView.PreviewMouseWheel -= m_ViewModel.CommanLibraryUI.MouseWheelChangeSize;
            OutputTextBox.PreviewMouseWheel -= m_ViewModel.CommanLibraryUI.MouseWheelChangeSize;
        }
        void ListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            // 检查点击的是否是空白区域
            if (sender is ListView listView)
            {
                if (!ClickedItem<ListViewItem>(listView, e.GetPosition(listView)))
                {
                    // 点击的是空白区域，取消当前选择
                    listView.SelectedItem = null;
                }
            }
        }
        // 扩展方法，用于获取ListView中指定位置的ListViewItem
        bool ClickedItem<T>(ListView listView, Point point)
        {
            return (listView.InputHitTest(point) is T);
        }
        #region IAimoWindow

        bool _ClosedActived = false;
        bool IAiMoWindow.CloseActived => _ClosedActived;
        void IAiMoWindow.UpdateState()
        {
            m_ViewModel.CommanLibraryUI.UpdateAllUIWindows();
            m_ViewModel.CommanLibraryUI.UpdateAttention();
        }
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            RemoveEvent();
            _ClosedActived = true;
        }
        void IDebugPrinter.PrintDebug(string text)
        {
            PrintDebug(text);
        }
        public void ClearPrint()
        {
            if (!_ClosedActived)
            {
                OutputTextBox.Dispatcher.InvokeAsync(() =>
                {
                    OutputTextBox.Clear();
                });
            }
        }
        public void PrintDebug(string message)
        {
            if (!_ClosedActived && !string.IsNullOrEmpty(message))
            {
                OutputTextBox.Dispatcher.InvokeAsync(() =>
                {
                    OutputTextBox.AppendText($"[{DateTime.Now:HH:mm:ss}] " + message + Environment.NewLine);
                    if (OutputTextBox.LineCount > 300)
                    {
                        int index = OutputTextBox.GetCharacterIndexFromLineIndex(OutputTextBox.LineCount - 300);
                        OutputTextBox.Text = OutputTextBox.Text[index..];
                    }
                    OutputTextBox.ScrollToEnd();
                });
            }
        }
        #endregion

    }
}
